import { DbService } from '@app/db';
import { Injectable } from '@nestjs/common';
import { In } from 'typeorm';

@Injectable()
export class RoleService {
    constructor(
        public readonly db:DbService
    ){}

    async getMemberRoleMenu(userId:number) {
        const roleIds = await this.db.member.value({id : userId}, 'roles') as number[]
        if(!roleIds || roleIds.length == 0) return null

        let roles = await this.db.role.find({id : In(roleIds), enable : true}, ['menus'])

        let menuIdsSet = new Set<number>()

        roles.forEach(role => {
            if(role.menus) {
                role.menus.forEach(menu => menuIdsSet.add(menu))
            }
        })

        return menuIdsSet.size == 0 ? null : Array.from(menuIdsSet)
    }
}
